/*
 * @Author: zi.yang
 * @Date: 2021-05-21 10:08:02
 * @LastEditors: zi.yang
 * @LastEditTime: 2021-10-03 18:26:37
 * @Description: 挂载 Vue 全局对象
 * @FilePath: \ziYangPM\src\Utils\global.js
 */
/* eslint-disable no-param-reassign */
import moment from 'moment'

import request from '@/apis'
import router from '@/router'

import validate from './validate'

/**
 * Vue 全局对象统一以 $ 开头命名
 */
export default {
  install (Vue) {
    // api 请求接口
    Vue.prototype.$request = request

    // 当前接口地址前缀
    Vue.prototype.$prefix = '/api'

    // 当前运行环境
    Vue.prototype.$env = process.env.VUE_APP_ENV

    // 常用正则封装
    Vue.prototype.$validate = validate

    /**
     * 检查类型
     * @param {*} type
     * @returns { Boolean }
     */
    Vue.prototype.$isType = (type) => (obj) => {
      return {}.toString.call(obj) === `[object ${type}]`
    }

    /**
     * 添加默认选项：全部
     * @param { Array } options 原 option 列表
     * @param { Object } option 默认 option 列表
     * @returns { Array } newOption
     */
    Vue.prototype.$addDefaultOption = (
      options,
      option = { dataValue: '-1', dataLabel: '全部' }
    ) => {
      const newOption = JSON.parse(JSON.stringify(options))
      newOption.unshift(option)
      return newOption
    }

    /**
     * 路由跳转
     * @method $routerLink
     * @param { String } path 路由路径
     * @param { String } type 类型 (list：列表，edit:修改，save：新增)
     * @param { Object } queryParams 请求参数
     */
    Vue.prototype.$routerLink = (path, type, queryParams) => {
      let params = { type }
      params = Object.assign(params, queryParams)
      // 路由
      router.push({ path, query: params })
    }

    /**
     * 下划线转驼峰
     * @method $camelCase
     * @param { String } str
     * @returns { String }
     * @description 'user-name' to 'userName'
     */
    Vue.prototype.$camelCase = (str) => str.replace(/-[a-z]/g, (s) => s.substr(-1).toUpperCase())

    /**
     * 常用时间格式化
     * @method $date
     * @param { String } time 时间戳
     * @param { String } format 转换格式 [default: YYYY-MM-DD HH:mm:ss]
     */
    Vue.prototype.$dateformat = (str, type = 'YYYY-MM-DD HH:mm:ss') => moment(str).format(type)

    /**
     * 获取时间差
     * @method $getDateDiff
     * @param { String } timespan 时间戳
     */
    Vue.prototype.$getDateDiff = function getDateDiff (timespan) {
      const dateTime = new Date(timespan)
      const year = dateTime.getFullYear()
      const month = dateTime.getMonth() + 1
      const day = dateTime.getDate()
      const hour = dateTime.getHours()
      const minute = dateTime.getMinutes()
      // var second = dateTime.getSeconds()
      const now = new Date()
      const nowNew = now.getTime()
      let milliseconds = 0
      let timeSpanStr
      milliseconds = nowNew - timespan

      if (milliseconds <= 1000 * 60 * 1) {
        timeSpanStr = '刚刚'
      } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
        timeSpanStr = `${Math.round(milliseconds / (1000 * 60))}分钟前`
      } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
        timeSpanStr = `${Math.round(milliseconds / (1000 * 60 * 60))}小时前`
      } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 3) {
        timeSpanStr = `${Math.round(milliseconds / (1000 * 60 * 60 * 24))}天前`
      } else if (milliseconds > 1000 * 60 * 60 * 24 * 3 && year === now.getFullYear()) {
        timeSpanStr = `${year}-${month}-${day} ${hour}:${minute}`
      } else {
        timeSpanStr = `${year}-${month}-${day} ${hour}:${minute}`
      }
      return timeSpanStr
    }
  }
}
